package com.lib.audioDecoder.player;

import com.lib.audioDecoder.G711Utils;

public class G711Player {

    public enum G711Type{
        G711_TYPE_A,
        G711_TYPE_U;
    }

    private final G711Type mType;
    private final short[] mPCMBuffer;
    private final PCMPlayer mPCMPlayer;

    public G711Player(G711Type g711Type,int buffer,int channelNum){
        this.mType = g711Type;
        this.mPCMPlayer = new PCMPlayer(buffer,8000,channelNum);
        this.mPCMBuffer = new short[buffer];
    }

    public void play(byte[] data){
        play(data,0,data.length);
    }

    public void play(byte[] data,int offset,int size){
        if (mType == G711Type.G711_TYPE_A){
            G711Utils.g711A2PCM(data,mPCMBuffer,offset,size);
        }else if (mType == G711Type.G711_TYPE_U){
            G711Utils.g711u2PCM(data,mPCMBuffer,offset,size);
        }

        mPCMPlayer.play(mPCMBuffer,size);
    }

    public void close(){
        mPCMPlayer.stop();
    }
}
